package common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.entity.User;
import common.mapper.UserMapper;
import common.service.UserService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;

    @Override
    public User findById(String id) {
        QueryWrapper<User>wrapper = new QueryWrapper<User>();
        wrapper.eq("id",id);
        User user =mapper.selectOne(wrapper);
        return user;
    }

    @Override
    public void createUser(User user) {
        //随机生成salt值，MD5
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        String password = new SimpleHash("md5",user.getPassword(),salt,2).toString();
        user.setPassword(password);
        mapper.insert(user);
    }
}
